//
//  SCTool.m
//  Scalper
//
//  Created by 王云 on 2017/9/14.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "SCTool.h"
#import "LCProgressHUD.h"
#import "HWAlertView.h"

#import "TYAlertView.h"
#import "TYAlertController.h"

//#import "SCLoginController.h"
#import "FirstViewController.h"
@implementation SCTool


+ (void)showAlerts:(NSString *)str
{
    HWAlertView *alertView = [[HWAlertView alloc] initWithTitle:nil andMessage:str];
    [alertView addButtonWithTitle:@"确定"
                             type:HWAlertViewButtonTypeCancel
                          handler:^(HWAlertView *alert) {
                              
                          }];
    
    alertView.transitionStyle = HWAlertViewTransitionStyleDropDown;
    [alertView show];
    
}

+ (void)showAlerts:(NSString *)str Ok:(void (^)(id))Ok
{
    HWAlertView *alertView = [[HWAlertView alloc] initWithTitle:nil andMessage:str];
    
    [alertView addButtonWithTitle:@"确定"
                             type:HWAlertViewButtonTypeCancel
                          handler:^(HWAlertView *alert) {
                              Ok(alert);
                          }];
    
    alertView.transitionStyle = HWAlertViewTransitionStyleDropDown;
    [alertView show];
    
}

+ (void)showAlerts:(NSString *)str Ok:(void (^)(id))Ok Cancel:(void (^)(id))Cancel
{
    
    HWAlertView *alertView = [[HWAlertView alloc] initWithTitle:nil andMessage:str];
    [alertView addButtonWithTitle:@"取消"
                             type:HWAlertViewButtonTypeCancel
                          handler:^(HWAlertView *alertView) {
                              Cancel(alertView);
                          }];
    [alertView addButtonWithTitle:@"确定"
                             type:HWAlertViewButtonTypeDestructive
                          handler:^(HWAlertView *alertView) {
                              Ok(alertView);
                          }];
    alertView.cornerRadius = 10;
    alertView.buttonFont = [UIFont boldSystemFontOfSize:15];
    alertView.transitionStyle = HWAlertViewTransitionStyleDropDown;
    [alertView show];
    
}

/** 提示 */
+ (void)showAlerts:(NSString *)title msg:(NSString *)msg Ok:(void (^)(id alerts))Ok Cancel:(void (^)(id alerts))Cancel
{
    HWAlertView *alertView = [[HWAlertView alloc] initWithTitle:title andMessage:msg];
    [alertView addButtonWithTitle:@"取消"
                             type:HWAlertViewButtonTypeCancel
                          handler:^(HWAlertView *alertView) {
                              Cancel(alertView);
                          }];
    [alertView addButtonWithTitle:@"确定"
                             type:HWAlertViewButtonTypeDestructive
                          handler:^(HWAlertView *alertView) {
                              Ok(alertView);
                          }];
    alertView.cornerRadius = 10;
    alertView.buttonFont = [UIFont boldSystemFontOfSize:15];
    alertView.transitionStyle = HWAlertViewTransitionStyleDropDown;
    [alertView show];
    
    
}


/** 提示 */
+ (void)showAlerts:(NSString *)title
               msg:(NSString *)msg
                Ok:(void (^)(id alerts))Ok
           okTitle:(NSString *)okTitle
            cancel:(void (^)(id alerts))cancel
       cancelTitle:(NSString *)cancelTitle
{
    HWAlertView *alertView = [[HWAlertView alloc] initWithTitle:title andMessage:msg];
    [alertView addButtonWithTitle:cancelTitle
                             type:HWAlertViewButtonTypeCancel
                          handler:^(HWAlertView *alertView) {
                              cancel(alertView);
                          }];
    [alertView addButtonWithTitle:okTitle
                             type:HWAlertViewButtonTypeDestructive
                          handler:^(HWAlertView *alertView) {
                              Ok(alertView);
                          }];
    alertView.cornerRadius = 10;
    alertView.buttonFont = [UIFont boldSystemFontOfSize:15];
    alertView.transitionStyle = HWAlertViewTransitionStyleDropDown;
    [alertView show];
    
    
}

/** 输入框  多个*/
+ (void)showAlerts:(NSString *)str list:(NSArray *)list dong:(void (^)(NSInteger tag))Ok
{
    HWAlertView *alertView = [[HWAlertView alloc] initWithTitle:nil andMessage:str];
    NSInteger idx = 0;
    for (NSString *str in list) {
        [alertView addButtonWithTitle:str
                                 type:HWAlertViewButtonTypeCancel
                              handler:^(HWAlertView *alertView) {
                                  Ok(idx);
                              }];
        idx++;
    }
    [alertView show];
    
}

/** 输入框  单个 按钮*/
+ (void)showAlerts:(NSString *)title
             input:(void (^)(UITextField *textField))tf
           okTitle:(NSString *)okTitle
                Ok:(void (^)(NSString *))Ok
{
    TYAlertView *alertView = [TYAlertView alertViewWithTitle:title message:nil];
    [alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        tf(textField);
    }];
    
    [alertView addAction:[TYAlertAction actionWithTitle:@"取消" style:TYAlertActionStyleCancle handler:^(TYAlertAction *action) {}]];
    
    __typeof (alertView) __weak weakAlertView = alertView;
    [alertView addAction:[TYAlertAction actionWithTitle:okTitle style:TYAlertActionStyleDestructive handler:^(TYAlertAction *action) {
        for (UITextField *textField in weakAlertView.textFieldArray) {
            Ok(textField.text);
        }
    }]];
    
    TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleAlert];
    [[UIWindow hw_optimizedVisibleViewController] presentViewController:alertController animated:YES completion:nil];;
    
    
}



#pragma mark - hub
+ (void)hideHUD
{
    [LCProgressHUD hide];
}

+ (void)showLoading:(NSString*)message
{
    [LCProgressHUD showLoading:message];
}

+ (void)showSuccess:(NSString *)success
{
    [LCProgressHUD showSuccess:success];
}

+ (void)showError:(NSString *)error
{
    [LCProgressHUD showFailure:error];
}

+ (void)showInfo:(NSString *)message
{
    [LCProgressHUD showInfoMsg:message];
}

+ (void)showMessage:(NSString *)message
{
    [LCProgressHUD showMessage:message];
}

+ (void)loginVc:(UIViewController *)vc{
    
    JTNavigationController *loginNav = [[JTNavigationController alloc]initWithRootViewController:[[FirstViewController alloc]init]];
    [vc presentViewController:loginNav animated:YES completion:^{
        
    }];
//    [UIApplication sharedApplication].keyWindow.rootViewController =[[RTRootNavigationController alloc] initWithRootViewController:[[FirstViewController alloc] init]];
}


+ (void)showSetting{
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"prefs:root=Privacy"]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy"]];
    }else{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }
}


+ (void)openPhoneOrUI{
    
    UIWebView *callWebview =[[UIWebView alloc] init];
    NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",[SCUserTool loadInfo].bd_phone]];
    [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
    
    if ([[UIApplication sharedApplication] canOpenURL:telURL]){
        [[UIApplication sharedApplication] openURL:telURL options:@{} completionHandler:^(BOOL success) {
        }];
    }
    
}
+ (void)currentVersion:(void (^)(BOOL isNew))isNewVersion
{
    NSString *currentVersion = [HWCommon getCurrentVersion];
    if ([[ZYUserDefaults valueForKey:@"CFBundleVersion"] isEqual:currentVersion]) {
        isNewVersion(NO);
    }else{
        isNewVersion(YES);
        [ZYUserDefaults setObject:currentVersion forKey:@"CFBundleVersion"];
        [ZYUserDefaults synchronize];
    }
    
}

+ (void)checkAppUpdate{
    __weak __typeof(&*self)weakSelf = self;
    [SCApiTool SCversionSuccess:^(NSDictionary *dic) {
        [weakSelf setAppVers:dic];
    } failure:^(NSString *errormsg) {
        
    }];
}
+ (void)setAppVers:(NSDictionary *)dic{
    
    NSString *nowVersion = [HWCommon getCurrentShortVersion];
    NSString *v_code = [dic hw_safeObjectForKey:@"v_code"];
    if( [nowVersion compare:v_code options:NSNumericSearch]== -1){
        if ([[dic hw_safeObjectForKey:@"must_update"] boolValue]) {
            NSString *msg = [NSString stringWithFormat:@"你当前的版本是V%@，发现新版本V%@，如果不升级会影响你的正常使用，请升级",[HWCommon getCurrentShortVersion],[dic hw_safeObjectForKey:@"v_code"]];
            NSString *artistViewUrl = [dic hw_safeObjectForKey:@"v_url"];
            [SCTool showAlerts:@"升级提示" msg:msg Ok:^(id alerts) {
                NSURL *url = [NSURL URLWithString:artistViewUrl];
                [[UIApplication sharedApplication] openURL:url];
                [SCTool setAppVers:dic];
            } okTitle:@"现在升级" cancel:^(id alerts) {
                NSURL *url = [NSURL URLWithString:artistViewUrl];
                [[UIApplication sharedApplication] openURL:url];
                [SCTool setAppVers:dic];
            } cancelTitle:@"还是去升级吧"];
        }else{
            NSString *msg = [NSString stringWithFormat:@"你当前的版本是V%@，发现新版本V%@，是否下载新版本？",[HWCommon getCurrentShortVersion],[dic hw_safeObjectForKey:@"v_code"]];
            NSString *artistViewUrl = [dic hw_safeObjectForKey:@"v_url"];
            [SCTool showAlerts:@"升级提示" msg:msg Ok:^(id alerts) {
                NSURL *url = [NSURL URLWithString:artistViewUrl];
                [[UIApplication sharedApplication] openURL:url];
            } okTitle:@"现在升级" cancel:^(id alerts) {
                
            } cancelTitle:@"下次再说"];
        }
    }
}


@end
